//
//  HeanderView.m
//  你的温实初
//
//  Created by lanou on 16/7/13.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "HeanderView.h"
#define sfwidth self.frame.size.width
#define sfheight self.frame.size.height

@interface HeanderView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collection;

@property (nonatomic, strong) UITextField *textfield;

@property (nonatomic, strong) UIButton *taskButton;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) NSArray *collectionArray;

@end

@implementation HeanderView

- (NSArray *)collectionArray {
    if (!_collectionArray) {
        _collectionArray = @[@"母婴", @"心理", @"养生", @"俩性"];
    }
    return _collectionArray;
}

- (UILabel *)label {
    if (!_label) {
        _label = [[UILabel alloc] init];
        _label.text = @"健康资讯📰";
        _label.backgroundColor = [UIColor grayColor];
    }
    return _label;
}

- (UIButton *)taskButton {
    if (!_taskButton) {
        _taskButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, sfheight, sfheight)];
        [_taskButton setTitle:@"➕我的健康计划" forState:UIControlStateNormal];
        [_taskButton addTarget:self action:@selector(taskButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _taskButton.backgroundColor = [UIColor whiteColor];
        [_taskButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _taskButton.layer.cornerRadius = 10;
        [_taskButton.layer setBorderWidth:2];
        [_taskButton.layer setBorderColor:[UIColor greenColor].CGColor];
        [_taskButton.layer setMasksToBounds:YES];
    }
    return _taskButton;
}

- (UITextField *)textfield {
    if (!_textfield) {
        _textfield = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, sfwidth, sfheight)];
        _textfield.text = @"网络加载失败/(ㄒoㄒ)/~~";
        _textfield.textAlignment = NSTextAlignmentCenter;
        _textfield.backgroundColor = [UIColor grayColor];
    }
    return _textfield;
}

- (UICollectionView *)collection {
    if (!_collection) {
        UICollectionViewFlowLayout *flowlaout = [[UICollectionViewFlowLayout alloc] init];
        flowlaout.itemSize = CGSizeMake(sfwidth/6, sfwidth/6);
        flowlaout.sectionInset = UIEdgeInsetsMake(10, sfwidth/12, 10, sfwidth/12);
        _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, sfwidth, sfheight) collectionViewLayout:flowlaout];
        _collection.backgroundColor = [UIColor whiteColor];
        _collection.delegate = self;
        _collection.dataSource = self;
        
    }
    return _collection;
}

- (instancetype)initWithFrame:(CGRect)frame ImageArray:(NSMutableArray *)imageArray {
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor whiteColor];
    __weak typeof(self) weakSlef = self;
    LTScrollView *scrollview = [[LTScrollView alloc] initWithFrame:CGRectMake(0, 0, sfwidth, sfheight/3) imageArray:imageArray];
    [self addSubview:scrollview];
    [self addSubview:self.collection];
    [self.collection mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scrollview.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.size.height.mas_equalTo(scrollview.frame.size.height/2);
    }];
    
    [self.collection registerNib:[UINib nibWithNibName:@"FirstCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"firstcollection_cell"];
    
    [self addSubview:self.textfield];
    [self.textfield mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSlef.collection.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.size.height.mas_equalTo(weakSlef.frame.size.height/3);
    }];
    
    [self addSubview:self.taskButton];
    [self.taskButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSlef.textfield.mas_bottom);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.size.height.mas_equalTo(weakSlef.frame.size.height/12);
    }];
    
    [self addSubview:self.label];
    [self.label mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSlef.taskButton.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.size.height.mas_equalTo(weakSlef.frame.size.height/12);
    }];
    
    return self;
}

// 提醒事件
- (void)taskButtonAction:(UIButton *)sender {
    [self.FirstPage.navigationController pushViewController:[TaskViewController new] animated:YES];
}

#pragma mark ------------collectionview----------

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.collectionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"firstcollection_cell" forIndexPath:indexPath];
    cell.collectionCellLabel.text = self.collectionArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
            [self.FirstPage.navigationController pushViewController:[TakeCareBabyViewController new] animated:YES];
            break;
            case 1:
            [self.FirstPage.navigationController pushViewController:[HeartViewController new] animated:YES];
            break;
            case 2:
            [self.FirstPage.navigationController pushViewController:[HealthViewController new] animated:YES];
            break;
            case 3:
            [self.FirstPage.navigationController pushViewController:[SexViewController new] animated:YES];
            break;
            
        default:
            break;
    }
}

@end
